package com.open.capacity.controller;

import com.github.pagehelper.PageInfo;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.web.Result;
import com.open.capacity.model.CommentModel;
import com.open.capacity.model.UserModel;
import com.open.capacity.service.CommentService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: y_022
 * @date: 18:26-2021/6/24
 * @description:关于用户评论操作的controller！
 */
@Slf4j
@Api(tags = "COMMENT API")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 按用户id查询用户评价列表
     */
    @GetMapping("/findCommentByUserId")
    public Result findCommentByUserId(UserModel userModel,int page,int limit) {
        userModel.setLimits(limit);
        userModel.setPagees(page);
        PageInfo<CommentModel> topicList = commentService.findCommentByUserId(userModel);
        return Result.succeed(topicList, "操作成功!");
    }

    /**
     * 用户新增评论
     */
    @PostMapping("/addComment")
    public Result addComment(CommentModel commentModel) {
        try {
            commentService.addComment(commentModel);
            return Result.succeed("操作成功！");
        } catch (Exception e) {
           log.error("新增评论异常",e);
        }
        return Result.failed("操作失败！");
    }

    /**
     * 按用户id删除评论
     */
    @PostMapping("/deleteComment")
    public Result deleteComment(CommentModel commentModel) {
        try {
            commentService.deleteComment(commentModel);
            return Result.succeed("操作成功！");
        } catch (Exception e) {
            throw new ControllerException(e);
        }
//        return Result.failed("操作失败！");
    }

    @GetMapping("/findComment")
    public Result findComment(CommentModel commentModel){
        try{
            int count = commentService.findComment(commentModel);
            return Result.succeed(count,"操作成功！");
        }catch (Exception e){
            log.error("评价列表查询失败！",e);
        }
        return Result.succeed("操作失败！");
    }
}
